package com.lagou.goods.controller;

import com.lagou.common.pojo.Goods;
import com.lagou.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Value("${server.port}")
    private String port;

    @Autowired
    private GoodsService goodsService;

    /**
     * 通过id查询商品
     * @param id
     * @return
     */
    @GetMapping("getGoodsById/{id}")
    public Goods getGoodsById(@PathVariable("id") Integer id){
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println("port = " + port);
        return goodsService.getGoodsById(id);
    }

    /**
     * 通过id删除商品
     * @param id
     * @return
     */
    @GetMapping("deleteGoodsById/{id}")
    public Integer deleteGoodsById(@PathVariable("id")Integer id){
        System.out.println("port = " + port);
        return goodsService.deleteGoodsById(id);
    }

    /**
     * 通过id编辑/新增商品
     * @param goods
     * @return
     */
    @PostMapping("updateGoodsById")
    public Integer updateGoodsById(@RequestBody Goods goods){
        System.out.println("port = " + port);
        return goodsService.updateGoodsById(goods);
    }

    /**
     * 分页查询
     * pageSize = 10
     * 查询条件：商品名称、分类、价格范围、库存范围、上架状态
     * 返回类型：Map（list:商品集合、total:总条目数）
     * @param name
     * @param type
     * @param priceS
     * @param priceE
     * @param stockS
     * @param stockE
     * @param status
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("getAllGoods")
    public Map<List<Goods>, Integer> getAllGoods(String name, String type, Double priceS, Double priceE,
                                                  Integer stockS, Integer stockE, Integer status,
                                                  Integer pageIndex, Integer pageSize){
        System.out.println("port = " + port);
        Map<List<Goods>, Integer> allGoods = goodsService.getAllGoods(name, type, priceS, priceE, stockS, stockE, status, pageIndex, pageSize);
        return allGoods;
    }
}
